//
//  YXTabBarController.m
//  彩票
//
//  Created by mac on 15-3-24.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "YXTabBarController.h"
#import "YXTabBar.h"
#import "YXTabBarButton.h"
#import "YXNavigationController.h"

@interface YXTabBarController () <YXTabBarDelegate>

@property (nonatomic, weak) YXTabBar *customBar;

@end

@implementation YXTabBarController

-(void)tabBar:(YXTabBar *)tabBar from:(NSInteger)fromIndex to:(NSInteger)toIndex
{
    // 切换控制器
    // 取出当前选中的控制器
   /*方法一
    UINavigationController *nav = self.childViewControllers[btn.tag];
    self.selectedViewController =nav;
    */
    // 方法二
    self.selectedIndex = toIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加自定义view替换掉tabar
    [self addAndRemoveTabar];
    // 添加子控制器
    [self setup];
   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    for (UIView *childView in self.tabBar.subviews) {
        if (![childView isKindOfClass:[YXTabBar class]]) {
            [childView removeFromSuperview];
        }
    }
}

- (void)addAndRemoveTabar
{
    YXTabBar *tabBar = [[YXTabBar alloc] init];
    // 设置代理
    
    tabBar.frame = self.tabBar.bounds;
    tabBar.delegate = self;
    
    [self.tabBar addSubview:tabBar];
    self.customBar =  tabBar;
    // [self.tabBar removeFromSuperview];
    
}

- (void)setup
{
    [self setChildViewControllerWithName:@"YXHallStoryboard" andTitle:@"购票大厅" andNorImage:@"TabBar_LotteryHall" andSeletedImage:@"TabBar_LotteryHall_selected" ];
    [self setChildViewControllerWithName:@"YXArenaStoryboard" andTitle:@"竞技场"
     andNorImage:@"TabBar_Arena" andSeletedImage:@"TabBar_Arena_selected"];
    [self setChildViewControllerWithName:@"HYDiscoveryStoryboard" andTitle:@"发现" andNorImage:@"TabBar_Discovery" andSeletedImage:@"TabBar_Discovery_selected"];
    [self setChildViewControllerWithName:@"YXHistoryStoryboard" andTitle:@"开奖信息" andNorImage:@"TabBar_History" andSeletedImage:@"TabBar_History_selected"];
    [self setChildViewControllerWithName:@"YXMineStoryboard" andTitle:@"我的彩票" andNorImage:@"TabBar_MyLottery" andSeletedImage:@"TabBar_MyLottery_selected"];
}

- (void)setChildViewControllerWithName:(NSString *)name andTitle:(NSString *)title andNorImage:(NSString *)norImage andSeletedImage:(NSString *)seletedImage;
{
    // 加载UIStoryboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:name bundle:nil];
    // 创建初始化控制器
    YXNavigationController *nav = storyboard.instantiateInitialViewController;

    // 拿到栈顶控制器
    UIViewController *vc = nav.topViewController;
    
//    vc.navigationItem.title = title;
//    nav.tabBarItem.title = title;
     vc.title = title; // 顶上面两句
    vc.tabBarItem.image = [UIImage imageNamed:norImage];
    vc.tabBarItem.selectedImage =[UIImage imageNamed:seletedImage];

    [self addChildViewController:nav];
    [self.customBar addTabBarButton:vc.tabBarItem];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
